package com.tang4j.controller.permission;

import com.tang4j.core.controller.AbstractController;
import com.tang4j.core.model.ResponseModel;
import com.tang4j.core.model.easyui.TreeGrid;
import com.tang4j.core.model.factory.ResponseModelFactory;
import com.tang4j.core.util.DataUtil;
import com.tang4j.model.factory.TreeGridFactory;
import com.tang4j.model.response.ThSysUserDto;
import com.tang4j.service.ThSysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class ThSysUserController extends AbstractController<ThSysUserService, ThSysUserDto> {

    public ThSysUserController(ThSysUserService thSysUserService) {
        super(thSysUserService);
    }


    /**
     * @author fjy
     * @since 2019-11-21
     * 父类controller自动实现了以下接口（如果子controller有@RequestMapping指定值 接口前要带上该值）
     * 分页加载数据： /getPageView
     * 查询详细信息：/selectView
     * 新增或修改信息：/updateView
     * 删除信息：/deleteView 接口
     * 如需要拓展改接口信息 可在子service的iml接口重写对应的方法
     * 例如重写getPageView重新加载多表联合查询的数据实现新的分页
     */


    @PreAuthorize("hasRole('SYS')")
    @PostMapping("/userInfo")
    public ResponseModel queryUserInfo() {
        logger.info("--------------加载/userInfo-----------------");
        return this.service.queryUserInfo();
    }

    //@PreAuthorize("hasRole('sys')" )
    @PostMapping("/selectWorkFlowManageInfo")
    public ResponseModel selectWorkFlowManageInfo() {
        logger.info("--------------加载/selectWorkFlowManageInfo-----------------");
        return this.service.selectWorkFlowManageInfo();
    }

    //@PreAuthorize("hasRole('sys')" )
    @PostMapping("/selectRolesView")
    public ResponseModel selectRolesView() {
        logger.info("--------------加载/selectWorkFlowManageRoles-----------------");
        return this.service.selectRolesView();
    }

    //@PreAuthorize("hasRole('sys')" )
    @PostMapping("/resetPassword")
    public ResponseModel resetPassword(@RequestBody ThSysUserDto userDto) {
        return this.service.resetPassword(userDto.getId());
    }

    @Override
    public ResponseModel updateView(@RequestBody ThSysUserDto dto) {
        try {
            this.service.updateUserDept(dto);
            return ResponseModelFactory.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseModelFactory.error(e.getMessage());
        }
    }

//    @PostMapping("/selectDeptUserList")
//    public ResponseModel selectDeptUserList(){
////        List<ThSysUserDto> thSysUserDtoList = this.service.selectDeptUserList();
////        //用户按照
////        Map<String,List<ThSysUserDto>> userMap = thSysUserDtoList.stream().collect(Collectors.groupingBy(ThSysUserDto::getDeptPId));
////
////        List<TreeGrid> deptTree = TreeGridFactory.createByThSysUserDtoWithDept(thSysUserDtoList);
////        //部门按照所在父部门分类
////        Map<String,List<TreeGrid>> deptMap = deptTree.stream().collect(Collectors.groupingBy(TreeGrid::getPId));
////       //顶级部门
////        List<TreeGrid> topDeptList = deptMap.get("0");
////        topDeptList.forEach( );
////
////        return ResponseModelFactory.OKWithData();
//    }
}
